local skel = fk.CreateSkill {
  name = "emo__xiangshou",
}

Fk:loadTranslationTable{
  ["emo__xiangshou"] = "相守",
  [":emo__xiangshou"] = "每当你或拥有“翼”的角色需要使用【闪】或打出一张牌时，另一方可以替其使用或打出之，然后两者各摸一张牌。",

  ["#emo__xiangshou-jink"] = "相守：你可以替 %src 使用【闪】，然后你与其各摸一张牌",
  ["#emo__xiangshou-resp"] = "相守：你可以替 %src 打出其需要的牌，然后你与其各摸一张牌",
  ["#emo__xiangshou-resp-card"] = "相守：你可以替 %src 打出【%arg】，然后你与其各摸一张牌",

  ["$emo__xiangshou1"] = "愿以彩翼，为君遮风雨、避灾祸。",
  ["$emo__xiangshou2"] = "身披彩翼，心有灵犀。",
}

skel:addEffect(fk.AskForCardUse, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and data.pattern and Exppattern:Parse(data.pattern):matchExp("jink") then
      local other = player.room:getPlayerById(player:getMark("emo__biyi"))
      return other and other:isAlive() and (target == other or target == player)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local user = (target == player) and room:getPlayerById(player:getMark("emo__biyi")) or player
    -- 不许使用转化，处理麻烦
    local cards = room:askToCards(user, {
      min_num = 1, max_num = 1, skill_name = skel.name, pattern = "jink", prompt = "#emo__xiangshou-jink:" .. target.id,
    })
    if #cards > 0 then
      event:setCostData(self, cards)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self)
    local user = (target == player) and room:getPlayerById(player:getMark("emo__biyi")) or player
    data.result = {
      from = target,
      card = Fk:getCardById(cards[1]),
      extra_data = { emo__xiangshou_draw = {user, target} } -- 有点危险吧，在这塞数据
    }
    -- 别在这里摸牌，延时摸
    --if not user.dead then user:drawCards(1, skel.name) end
    --if not target.dead then target:drawCards(1, skel.name) end
    return true
  end
})

skel:addEffect(fk.AskForCardResponse, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and data.pattern then
      local other = player.room:getPlayerById(player:getMark("emo__biyi"))
      return other and other:isAlive() and (target == other or target == player)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local user = (target == player) and room:getPlayerById(player:getMark("emo__biyi")) or player
    local prompt = "#emo__xiangshou-resp:" .. target.id
    if Fk.all_card_types[data.pattern] ~= nil then
      prompt = "#emo__xiangshou-resp-card:" .. target.id .. "::" .. data.pattern
    end
    local cards = room:askToCards(user, {
      min_num = 1, max_num = 1, skill_name = skel.name, pattern = data.pattern, prompt = prompt,
    })
    if #cards > 0 then
      event:setCostData(self, cards)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self)
    local user = (target == player) and room:getPlayerById(player:getMark("emo__biyi")) or player
    data.result = {
      from = target,
      card = Fk:getCardById(cards[1]),
      extra_data = { emo__xiangshou_draw = {user, target} }
    }
    return true
  end
})

skel:addEffect(fk.CardUsing, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and table.contains((data.extra_data or {}).emo__xiangshou_draw or {}, player)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player:drawCards(1, skel.name)
  end,
})

skel:addEffect(fk.CardResponding, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and table.contains((data.extra_data or {}).emo__xiangshou_draw or {}, player)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player:drawCards(1, skel.name)
  end,
})

return skel
